package net.chanibal.hala.client;

import java.util.Date;

import net.chanibal.hala.client.Server.ImprezaData;
import net.chanibal.hala.client.Server.TerminData;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.InvocationException;
import com.google.gwt.user.client.rpc.SerializableException;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class Impreza extends DialogBox {

	private HorizontalPanel panel;
	private VerticalPanel verticalPanel1;
	private VerticalPanel verticalPanel2;
	Integer id;

	public Impreza(Integer id) {
		this.id = id;
		System.out.println(id);
		// Set the dialog box's caption.
		setText("Impreza");
		panel = new HorizontalPanel();

		verticalPanel1 = new VerticalPanel();
		panel.add(verticalPanel1);
		panel.add(new HTML("<div>&nbsp;</div>"));
		verticalPanel2 = new VerticalPanel();
		panel.add(verticalPanel2);
		// DialogBox is a SimplePanel, so you have to set its widget property to
		// whatever you want its contents to be.
		setWidget(new Label("Wczytywanie danych"));

		Server.Util.getInstance().getImpreza(id, new PobierzImpreza());
		Server.Util.getInstance().getImprezaTerminy(id, new PobierzTerminy());
		center();
	}

	public void setError(String e) {
		VerticalPanel verticalPanel = new VerticalPanel();
		setWidget(verticalPanel);
		Label l = new Label(e);
		l.setStyleName("gwt-Label-Error");
		verticalPanel.add(l);
		verticalPanel.add(new Button("Zamknij", new ClickListener() {
			public void onClick(Widget sender) {
				hide();
			}
		}));
		setWidget(verticalPanel);
		center();
	}

	class PobierzTerminy implements AsyncCallback {

		public void onFailure(Throwable caught) {
			try {
				throw caught;
			} catch (IncompatibleRemoteServiceException e) {
				// this client is not compatible with the server; cleanup and
				// refresh the
				// browser
				setError(e.toString());
			} catch (InvocationException e) {
				// the call didn't complete cleanly
				setError("Błąd połączenia:" + e.toString());
			} catch (SerializableException e) {
				// one of the 'throws' from the original method
				setError(e.getMessage());
			} catch (Throwable e) {
				// last resort -- a very unexpected exception
				setError(e.getLocalizedMessage());
			}
		}

		public void onSuccess(Object result) {
			String[][] data = (String[][]) result;
			 if(data.length>0){
				 verticalPanel2.add(new Label("Terminy do zarezerwowania:"));
				 for(int x=0;x<data.length;x++){
					 verticalPanel2.add(new Label(
					 data[x][0]+" - "
					 +data[x][1]));
				 }
					      
				 }else{
					 verticalPanel2.add(new Label("Brak możliwości rezerwacji"));
				 }
			 center();
		}

	}

	class PobierzImpreza implements AsyncCallback {

		public void onFailure(Throwable caught) {
			try {
				throw caught;
			} catch (IncompatibleRemoteServiceException e) {
				// this client is not compatible with the server; cleanup and
				// refresh the
				// browser
				setError("Błąd połączenia:" + e.getMessage());
			} catch (InvocationException e) {
				// the call didn't complete cleanly
				setError("Błąd połączenia:" + e.getMessage());
			} catch (SerializableException e) {
				// one of the 'throws' from the original method
				setError(e.getMessage());
			} catch (Throwable e) {
				// last resort -- a very unexpected exception
				setError(e.getMessage());
			}
		}

		public void onSuccess(Object result) {
			ImprezaData data = (ImprezaData) result;
			setWidget(panel);
			verticalPanel1.add(new Label("Tytuł:" + data.tytul));
			verticalPanel1.add(new Label("Wszystkich miejsc:" + data.miejsc));
			verticalPanel1
					.add(new Label("Wolnych miejsc:" + data.miejscWolnych));
			verticalPanel1.add(new Label("Koszt:" + data.kosztZaTermin));
			verticalPanel1.add(new HTML("<hr/>"));

			if (data.miejscWolnych.intValue() > 0) {
				verticalPanel1.add(new Button("Zarezerwuj",
						new ClickListener() {
							public void onClick(Widget sender) {
								if(Login.isLogined){
								
								Server.Util.getInstance().doReserve(id,
										new AsyncCallback() {

											public void onFailure(
													Throwable caught) {
												// TODO Auto-generated method
												// stub
												setError(caught.getMessage());
											}

											public void onSuccess(Object result) {
												// TODO Auto-generated method
												// stub
												History
														.onHistoryChanged(History
																.getToken());
												hide();
											}

										});
								}
								else{
									Login.refergoto = "Imprezy";
									hide();
									History.onHistoryChanged("MustLogin");
								}
							}
						}));
			} else {
				verticalPanel1.add(new Label("Brak miejsc wolnych"));
			}

			verticalPanel1.add(new Button("Zamknij", new ClickListener() {
				public void onClick(Widget sender) {
					hide();
				}
			}));

			center();
		}

	};

}
